package mm.chenchen.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import mm.chenchen.common.constant.SeqConstant;
import mm.chenchen.constant.ResultMsg;

import mm.chenchen.db.annotation.DataSource;
import mm.chenchen.modules.sys.dao.FastStartSeqDao;
import mm.chenchen.modules.sys.service.FastStartSeqService;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import mm.chenchen.utils.R;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import mm.chenchen.modules.sys.entity.FastStartDictInfo;
import mm.chenchen.modules.sys.dao.FastStartDictInfoDao;
import mm.chenchen.modules.sys.service.FastStartDictInfoService;
import mm.chenchen.utils.PageUtils;
import mm.chenchen.utils.PageModel;

import java.util.Map;

import mm.chenchen.modules.sys.dao.model.request.FastStartDictInfoQuery;
import org.springframework.util.ObjectUtils;


@Service("fastStartDictInfoService")
////@DataSource("base")
public class FastStartDictInfoServiceImpl extends ServiceImpl<FastStartDictInfoDao, FastStartDictInfo> implements FastStartDictInfoService {
    @Autowired
    private FastStartSeqService seqService;

    public R findPage(Page<Map> page, FastStartDictInfoQuery param) {
        IPage<FastStartDictInfo> pageData = baseMapper.findPage(page, param);
        if (pageData != null) {
            return R.ok().setData(new PageModel(pageData.getRecords(), pageData.getTotal(), pageData.getSize(), pageData.getCurrent()));
        } else {
            return R.error(ResultMsg.DATA_NOT_EXIT);
        }
    }

    public R getById(Integer id) {
        return R.ok().setData(super.getById(id));
    }


    public R saveInfo(FastStartDictInfo param) {
        param.setId(seqService.nextVal(SeqConstant.DICT_SEQ_ID_NAME));
        return R.ok().setData(super.save(param));
    }


    public R update(FastStartDictInfo param) {
        FastStartDictInfo data = super.getById(param.getId());
        if (ObjectUtils.isEmpty(data)) {
            return R.error(ResultMsg.DATA_NOT_EXIT);
        }
        return R.ok().setData(super.updateById(param));
    }


    public R deleteById(Integer id) {
        return R.ok().setData(super.removeById(id));
    }

    @Override
    public R getDropList(FastStartDictInfo param) {
        QueryWrapper<FastStartDictInfo> dictInfoQueryWrapper = new QueryWrapper<>();
        LambdaQueryWrapper<FastStartDictInfo> lambdaQueryWrapper = dictInfoQueryWrapper.lambda();
        if (!ObjectUtils.isEmpty(param.getDictType())) {
            lambdaQueryWrapper.eq(FastStartDictInfo::getDictType, param.getDictType());
        }
        if (!ObjectUtils.isEmpty(param.getStatus())) {
            lambdaQueryWrapper.eq(FastStartDictInfo::getStatus, param.getStatus());
        }
        return R.ok().setData(list(dictInfoQueryWrapper));
    }

    @Override
    public R getDictTypeList() {
        QueryWrapper<FastStartDictInfo> dictInfoQueryWrapper = new QueryWrapper<>();
        dictInfoQueryWrapper.select("DISTINCT type_name","dict_type");
        LambdaQueryWrapper<FastStartDictInfo> lambdaQueryWrapper = dictInfoQueryWrapper.lambda();
        //lambdaQueryWrapper.select(FastStartDictInfo::getDictType);
        return R.ok().setData(list(dictInfoQueryWrapper));    }
}